unit frmUsuario;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ExtCtrls, DBCtrls, StdCtrls, DBTables, IBCustomDataSet,
  IBQuery, Grids, DBGrids, Mask, ComCtrls, ToolWin;

type
  TFormUsuario = class(TForm)
    lbNome: TLabel;
    lbLogin: TLabel;
    lbSenha: TLabel;
    lbEndereco: TLabel;
    lbTelefone: TLabel;
    lbEmail: TLabel;
    lbSetor: TLabel;
    DBGrid1: TDBGrid;
    ToolBar1: TToolBar;
    BtUsuarioNovo: TToolButton;
    BtUsuarioEditar: TToolButton;
    ToolButton3: TToolButton;
    BtUsuarioExcluir: TToolButton;
    BtUsuarioSalvar: TToolButton;
    ToolButton6: TToolButton;
    BtUsuarioPrimeiro: TToolButton;
    BtUsuarioAnterior: TToolButton;
    BtUsuarioProximo: TToolButton;
    BtUsuarioUltimo: TToolButton;
    StatusBar1: TStatusBar;
    ComboBoxSetor: TComboBox;
    EditNome: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure PreencheComboBoxSetor;
    procedure BtUsuarioNovoClick(Sender: TObject);
    procedure BtUsuarioSalvarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormUsuario: TFormUsuario;
  ListaIdSetor : TStringList;  // lista com os Id's de setores

implementation

uses UDMCadastro, frmMain, UDMConexoes;

{$R *.dfm}

procedure TFormUsuario.FormCreate(Sender: TObject);
begin
  DMCadastro.QueryUsuario.Active:=true;
  DMCadastro.QuerySetor.Active:=true;
  PreencheComboBoxSetor;
end;

procedure TFormUsuario.PreencheComboBoxSetor;
begin
  ListaIdSetor := TStringList.Create;   // cria a lista p/ armazenar os Id's dos setores
  ComboBoxSetor.Items.Clear;

  with TIBQuery.Create(Application) do
  begin
    Name     := 'QueryPreencherSetor';
    Active   := False;
    Database := DMConexoes.BDDados;
    Close;
    SQL.Clear;
    SQL.Text := ' SELECT IDSETOR, NOME FROM SETOR ORDER BY NOME ' ;
    Open;
    First;
    while not Eof do  // percorre toda a tabela de Setores p/ preecher lista de Id's de Setores e ComboBox c/ nomes
    begin
      ListaIdSetor.Add(FieldByName('IDSETOR').AsString);
      ComboBoxSetor.Items.Add(FieldByName('NOME').AsString);
      Next;
    end;
    Free;
  end;
  ComboBoxSetor.ItemIndex := 0;
end;

procedure TFormUsuario.BtUsuarioNovoClick(Sender: TObject);
begin
  DMCadastro.QueryUsuario.Insert;
  DMCadastro.QueryUsuario.FieldByName('IDUSUARIO').AsInteger := FormMain.GetNextID('USUARIO', 'IDUSUARIO');
end;

procedure TFormUsuario.BtUsuarioSalvarClick(Sender: TObject);
begin
  With DMCadastro.QueryUsuario do
  begin
    if ((State = dsEdit) or (State = dsInsert)) then
    begin
      FieldByName('NOME').AsString := EditNome.Text;
      FieldByName('SETOR_IDSETOR').AsInteger := StrToInt(ListaIdSetor[ComboBoxSetor.ItemIndex]);
      Post;
    end;
  end;
end;

end.
